package com.example.amusic

import android.app.Application
import android.content.ComponentName
import android.os.Build
import androidx.media3.common.Player
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.disk.DiskCache
import coil.memory.MemoryCache
import coil.util.DebugLogger
import com.example.amusic.service.MusicService
import com.example.amusic.service.MusicServiceConnection
import com.example.amusic.service.PlayServiceModule
import com.example.amusic.service.PlayerController
import com.google.common.util.concurrent.MoreExecutors

class MyApp : Application(), ImageLoaderFactory {
    companion object {
        lateinit var ctx: Application
            private set

        val STORAGE_PERMISSION =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
                android.Manifest.permission.READ_MEDIA_AUDIO
            else
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE

//        lateinit var playerController: PlayerController
    }
    override fun onCreate() {
        super.onCreate()
        ctx = this
//        bindMediaSession()
    }

    private fun bindMediaSession() {
        val sessionToken = SessionToken(this, ComponentName(this, MusicService::class.java))
        val mediaControllerFuture = MediaController.Builder(this, sessionToken).buildAsync()
        mediaControllerFuture.addListener({mediaControllerFuture.get()}, MoreExecutors.directExecutor())
    }

    override fun newImageLoader() = ImageLoader.Builder(this).memoryCache {
        MemoryCache.Builder(this).maxSizePercent(0.20).build()
    }.diskCache {
        DiskCache.Builder()
            .directory(cacheDir.resolve("coil"))
            .maxSizeBytes(10 * 1024 * 1024L)
            .build()
    }.logger(DebugLogger()).respectCacheHeaders(false)
        .build()
}


//https://medium.com/@myofficework000/building-a-media-player-app-with-jetpack-media3-in-jetpack-compose-16ae8c35f955
//https://github.com/myofficework000/Musify
//https://github.com/myofficework000/Notifications-With-Compose
//https://github.com/Tariq2518/Media3MusicPlayerCompose.git

//https://github.com/wangchenyan/ponymusic  准备参考这个来进行修改播放模块

//https://github.com/TheMusicmax/Musicmax 这个好像合理点